package A1_栈和队列;

import java.util.Stack;

public class LC_1047删除字符串中的所有相邻重复项 {
    public static void main(String[] ars){
        String s="abbaca";
        System.out.println(removeDuplicates(s));
    }
    public static String removeDuplicates(String s) {
        Stack<Character> st=new Stack<>();
        char[] c=s.toCharArray();
        for(int i=0;i<s.length();i++){
            if(st.isEmpty()||st.peek()!=c[i])
                st.push(c[i]);
            else
                st.pop();
        }
        String str="";
        while(!st.isEmpty()){
            str+=st.pop();
        }
        return String.valueOf(new StringBuilder(str).reverse());
    }
}
